<?php
namespace app\controllers;
use Yii;
use yii\web\Controller;
use app\controllers\CommonController;
use app\models\User;
use app\models\Product;
use app\models\Cart;

class CartController extends CommonController
{
    public function actionIndex()
    {
        $this->layout="layout2.php";
        if(Yii::$app->session['User']['isLogin']!=1){
            return $this->redirect(['member/auth']);
        }
       $user= User::find()->where('username=:name',[':name'=> \Yii::$app->session['User']['username']])->one();
       $cart= Cart::find()->where('userid=:uid',[':uid'=>$user->userid])->asArray()->all();
       $data=[];
       foreach ($cart as $key=>$value){
           $product= Product::find()->where('productid=:pid',[':pid'=>$value['productid']])->asArray()->one();
           $data[$key]['cover']=$product['cover'];
           $data[$key]['title']=$product['title'];
           $data[$key]['productnum']=$value['productnum'];
           $data[$key]['price']=$value['price'];
           $data[$key]['productid']=$value['productid'];
           $data[$key]['cartid']=$value['cartid'];
       }
        return $this->render('index',['data'=>$data]);
    }
    
    public function actionAdd()
    {
        if(Yii::$app->session['User']['isLogin']!=1){
            return $this->redirect(['member/auth']);
        }
        $userid= User::find()->where('username=:name',[':name'=> \Yii::$app->session['User']['username']])->one();
        if(\Yii::$app->request->isPost){
            $post= \Yii::$app->request->post();
            $num= $post['productnum'];
            $data['Cart']=$post;
            $data['Cart']['userid']=$userid->userid;
//            var_dump($data);exit;
        }
        if(Yii::$app->request->isGet){
            $productid=Yii::$app->request->get('productid');
            $model= Product::find()->where('productid=:pid',[':pid'=>$productid])->one();
            $price=$model->issale ? $model->saleprice :$model->price;
            $num=1;
            $data['Cart']=['productid'=>$productid,'productnum'=>$num,'price'=>$price,'userid'=>$userid->userid];
        }
        //判断购物车中，有商品，则商品数量加1，否则加上商品
//        $model;
        if(!$model= Cart::find()->where('productid=:pid and userid=:uid',[':pid'=>$data['Cart']['productid'],':uid'=>$data['Cart']['userid']])->one()){
            $model=new Cart();
        }else{
            $data['Cart']['productnum']=$model->productnum+$data['Cart']['productnum'];
        }
        $data['Cart']['createtime']= time();
        $model->load($data);
        $model->save();
        return $this->redirect(['cart/index']);
    }
    
    public function actionMod()
    {
        $cartid= Yii::$app->request->get('cartid');
        $productnum=Yii::$app->request->get('productnum');
        Cart::updateAll(['productnum'=>$productnum],'cartid=:cid',['cid'=>$cartid]);
    }
    
    public function actionDel()
    {
        $cartid= Yii::$app->request->get('cartid');
        Cart::deleteAll('cartid=:cid',[':cid'=>$cartid]);
        return $this->redirect(['cart/index']);
    }
    
}
